import {
  key,login
} from "@/api/key"
const keys = {
  state: {
    code: "",
    dynamicCode:""//动态令牌
  },
  mutations: {
    ADD_KEY: (state, data) => {
      state.code = data;
    },
    CLEAR_KEY: (state, data) => {
      state.code = ""
    },
    DYNAMIC_KEY: (state, data) => {
      state.dynamicCode = data
    },
    DYNAMIC_CLEAR_KEY: (state, data) => {
      state.dynamicCode = ""
    },
  },
  actions: {
    CLEARKEY({
      commit,
      state
    }) {
      commit("CLEAR_KEY", "");
    },
    DYNAMIC_CLEARKEY({
      commit,
      state
    }) {
      commit("DYNAMIC_CLEAR_KEY", "");
    },
    DYNAMICKEY({
      commit,
      state
    }) {
      return new Promise((resolve, reject) => {
        login().then(res => {
          
          if (res) {
            commit("DYNAMIC_KEY", res);
            resolve(true)
          } else {
            resolve(false)
          }
        }).catch(err => {
          resolve(false)
        })
      })
    },
    GETKEY({
      commit,
      state
    }, info) {
      return new Promise((resolve, reject) => {
        key().then(res => {
          if (res) {
            commit("ADD_KEY", res);
            resolve(true)
          } else {
            resolve(false)
          }
        }).catch(err => {
          resolve(false)
        })
      })
    }
  }
}
export default keys
